function coef = im2jpeg(img, qf)
    qtable = jpeg_qtable(qf);
    [M,N] = size(img);
    P = ceil(M / 8)*8;
    Q = ceil(N / 8)*8;
    pad = zeros(P, Q);% padding
    pad(1:M, 1:N) = double(img); 
    clear img;
    T = dctmtx(8);
    fun = @(block_struct) round(T * block_struct.data * T' ./ qtable);
    coef = blockproc(pad - 128,[8 8], fun);
end
